package dimensional.traveler.client.mvc;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.sencha.gxt.core.client.util.ToggleGroup;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.form.Radio;

import dimensional.traveler.client.Mediator;
import dimensional.traveler.client.UserManagement;
import dimensional.traveler.client.services.CareerService;
import dimensional.traveler.client.services.CareerServiceAsync;
import dimensional.traveler.client.services.LoginService;
import dimensional.traveler.client.services.LoginServiceAsync;
import dimensional.traveler.client.services.UserService;
import dimensional.traveler.client.services.UserServiceAsync;
import dimensional.traveler.client.ui.util.MessagePopup;

public class ControllerUtils {

	public static void logout(final Mediator mediator, final MediatedController mediatedController) {
		UserManagement.logoutUser();

		LoginServiceAsync loginService = GWT.create(LoginService.class);

		loginService.logout(UserManagement.getLoggedInUser(), new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				mediator.displayLoginScreen();
			}

			@Override
			public void onFailure(Throwable caught) {
				mediatedController.handleRequestFailure(caught);
			}
		});
	}

	public static void loadGame(final Mediator mediator, final MediatedController mediatedController) {
		final Dialog loadDialog = new Dialog();

		loadDialog.setBodyBorder(false);
		loadDialog.setHeadingText("Load your game");
		loadDialog.setWidth(400);
		loadDialog.setHeight(225);
		loadDialog.setHideOnButtonClick(true);
		loadDialog.setModal(true);

		BorderLayoutContainer layout = new BorderLayoutContainer();
		loadDialog.add(layout);

		// center
		ContentPanel centerPanel = new ContentPanel();
		centerPanel.setHeadingText("Your saves");
		layout.setCenterWidget(centerPanel);

		final VerticalLayoutContainer saveGameContainer = new VerticalLayoutContainer();
		ScrollPanel scrollPanel = new ScrollPanel();
		scrollPanel.add(saveGameContainer);
		centerPanel.add(scrollPanel);

		final ToggleGroup toggle = new ToggleGroup();

		final UserServiceAsync userService = GWT.create(UserService.class);
		final CareerServiceAsync careerService = GWT.create(CareerService.class);

		userService.getSavedGamesForUser(UserManagement.getLoggedInUser(), new AsyncCallback<List<String>>() {
			@Override
			public void onSuccess(List<String> result) {
				saveGameContainer.clear();
				toggle.clear();

				for (String saveGame : result) {
					Radio radio = new Radio();
					radio.setBoxLabel(saveGame);
					toggle.add(radio);
					saveGameContainer.add(radio);
				}

				loadDialog.show();
			}

			@Override
			public void onFailure(Throwable caught) {
				mediatedController.handleRequestFailure(caught);
			}
		});

		loadDialog.addHideHandler(new HideHandler() {

			@Override
			public void onHide(HideEvent event) {
				if (loadDialog.getHideButton() != null && "OK".equals(loadDialog.getHideButton().getText())) {
					String saveGameName = ((Radio) toggle.getValue()).getBoxLabel();

					careerService.loadGame(UserManagement.getLoggedInUser(), saveGameName, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							mediatedController.handleRequestFailure(caught);
						}

						@Override
						public void onSuccess(Void result) {
							MessagePopup.displayInfoMessage("Game save", "Your game has been loaded");
							mediator.displayGameMenuScreen();
						}

					});

				}
			}
		});

	}

}
